package com.g17.virtualpetbackend.repo;

import com.g17.virtualpetbackend.entity.User;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends Neo4jRepository<User,Long> {

    @Query("MATCH (n:user) RETURN n")
    List<User> findAll();

    @Query("MATCH (n:user) WHERE id(n)=$id RETURN n")
    Optional<User> findById(Long id);

    @Query("MATCH (n:user) WHERE n.username=$username AND n.password=$password RETURN n")
    Optional<User> findByUsernameAndPassword(String username, String password);

    //new user is set to be student
    @Query("CREATE (n:user {username:$username, password:$password, type:0} ) RETURN n")
    User createUser(String username, String password);

    @Query("MATCH (n:user) WHERE id(n) = $id SET n = {username:$username, password:$password, type:$type} RETURN n")
    User updateUser(Long id, String username, String password, Integer type);

    @Query("MATCH (n:user) WHERE id(n) = $id DELETE n RETURN n")
    User deleteUserById(Long id);

    // To get all students who attend the test with indicated paper
    @Query("MATCH (u)-[:ATTEND]->(p:Paper) " +
            "WHERE id(p) = $paperId " +
            "RETURN u")
    List<User> getAttendedUserByPaper(Long paperId);

    @Query("MATCH (u:user) " +
            "WHERE u.username = $username " +
            "RETURN u")
    Optional<User> findAllUsersByUsername(String username);

    @Query("MATCH (u:user)-[a:ATTEND]->(p:Paper) " +
            "WHERE id(p) = $paperId " +
            "RETURN u")
    List<User> findAllUsersByPaperId(Long paperId);
}
